package so.wy.servicecenter.businessbed.service.hospital;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.exception.ServiceException;
import so.wy.servicecenter.base.frame.baseopt.BaseOptService;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_building.HospitalBuildingDAO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_building.HospitalBuildingDO;
import so.wy.servicecenter.businessbed.service.hospital.dto.HospitalBuildingDTO;
import so.wy.servicecenter.businessbed.service.hospital.dto.HospitalBuildingTreeDTO;
import so.wy.servicecenter.data.dao.enums.YesNoEnum;

import java.util.ArrayList;
import java.util.List;

@Service
@Scope("prototype")
public class HospitalBuildingService extends BaseOptService<HospitalBuildingDAO, HospitalBuildingDO, HospitalBuildingDTO> {

    @Override
    protected HospitalBuildingDTO convert2DTO(HospitalBuildingDO hospitalBuildingDO) {
        return new HospitalBuildingDTO(hospitalBuildingDO);
    }

    @Override
    protected HospitalBuildingDO convert2DO(HospitalBuildingDTO hospitalBuildingQuery) {
        return hospitalBuildingQuery.convert2DO();
    }

    @Override
    public HospitalBuildingDTO addRecord(HospitalBuildingDTO recordQuery) throws ServiceException {
        if (recordQuery.getParentId() != 0L) {
            HospitalBuildingDO parentDO = getById(recordQuery.getParentId());
            if (parentDO == null) {
                throw exception("父节点不存在!");
            } else if (parentDO.getLevel() == 2) {
                throw exception("父节点异常，不能添加子节点!");
            }
            //当前二级节点更新
            recordQuery.setBuildingId(parentDO.getBuildingId());
            recordQuery.setBuildingName(parentDO.getBuildingName());
            recordQuery.setLevel(2);

            //更新父节点信息
            parentDO.setIsNode(YesNoEnum.YES.getCode());
            updateById(parentDO);
        } else {
            recordQuery.setLevel(1);
        }

        recordQuery.setIsNode(YesNoEnum.NO.getCode());
        HospitalBuildingDO buildingDO = saveWithReturn(recordQuery.convert2DO());

        if (recordQuery.getLevel() == 1) {
            buildingDO.setBuildingId(buildingDO.getId());
            buildingDO.setBuildingName(buildingDO.getName());
        } else {
            buildingDO.setFloorId(buildingDO.getId());
            buildingDO.setFloorName(buildingDO.getName());
        }
        updateById(buildingDO);

        return new HospitalBuildingDTO(buildingDO);
    }

    public List<HospitalBuildingTreeDTO> getHospitalTree(Long hospitalId) {
        List<HospitalBuildingTreeDTO> tree = new ArrayList<>();

        List<HospitalBuildingDO> list = list(lambdaQuery().eq(HospitalBuildingDO::getHospitalId, hospitalId)
                .eq(HospitalBuildingDO::getParentId, 0L));
        for (HospitalBuildingDO buildingDO : list) {
            HospitalBuildingTreeDTO node = new HospitalBuildingTreeDTO(buildingDO);
            List<HospitalBuildingDO> children = list(lambdaQuery().eq(HospitalBuildingDO::getParentId,
                    buildingDO.getId()));
            List<HospitalBuildingTreeDTO> leafs = new ArrayList<>();
            for (HospitalBuildingDO child : children) {
                HospitalBuildingTreeDTO leaf = new HospitalBuildingTreeDTO(child);
                leaf.setChildren(new ArrayList<>());
                leafs.add(leaf);
            }
            node.setChildren(leafs);

            tree.add(node);
        }

        return tree;
    }
}
